﻿<?php
	error_reporting(0);
	$DBSERVER ="localhost";
	$DATABASENAME = "sqlecore";   //数据库
	$USERNAME = "ecore";         //数据库帐号
	$PASSWORD = "ecoredata";       //数据库密码
	
//	$SMSUSERNAME = "admin";
//	$SMSPASSWORD = "admin";
	//$SMSIPPAGE = "http://195.178.62./send.asp";
	
	$PRODUCTSPERPAGE = 10;
	$PRODUCTSPERPAGE_MYACCOUNT = 5;
	$BIDHISTORYPAGE_MYACCOUNT=10;   //我的帐户中每页要显示的记录数 
	//$Currency ="¥" ;//"&euro;";     //显示的货币符号
	//$SMSrate = 1.50;
	//$SMSsendnumber = "1065";
	$AllPageTitle = "Green City";
	$SITE_URL = "http://www.greencityshoes.com/";
	
	$ADMIN_MAIN_SITE_NAME="Green City";
	
 	if ($_SESSION["Language"]=="")
 	{
 		$_SESSION["Language"]="CHINESE";
 	}
 	
 	if ($_SESSION["Language"]=="CHINESE")
	{	
		include("language/china.php");
	}
	else if ($_SESSION["Language"]=="ENGLISH")
	{
		include("language/english.php");
	}	
		

/**
* MySQL读写分离类的实现
* $db_config = array(
*          'master' => array('host'=>'localhost','user'=>'ecore','passwd'=>'ecoredata','db'=>'sqlecore'),
*          'slave'  => array(
*                           array('host'=>'localhost','user'=>'ecore','passwd'=>'ecoredata','db'=>'sqlecore'),
*                           array('host'=>'localhost','user'=>'ecore','passwd'=>'ecoredata','db'=>'sqlecore'),
*                      )
*  );
*  
*  注释：如果slave有多个时随机连接其中的一个
*/
/*
$db_config = array(
    'master' => array('host'=>'localhost:3306','user'=>'admin','passwd'=>'123456','db'=>'stat'),
    'slave'  => array(
                    array('host'=>'localhost:3307','user'=>'admin','passwd'=>'123456','db'=>'stat'),
                    array('host'=>'localhost:3308','user'=>'admin','passwd'=>'123456','db'=>'stat')
                )
);
$db = MySQL::getInstance('','r-w');
$sql = "select * from admin";
$rs = $db->query($sql);
while ($row = $db->fetch($rs)){
    echo "uid:".$row['uid']."   ".$row['userName']."<br />";
}
echo "<hr />";
*/



//	$db_config = array(
//	    'master' => array('host'=>'localhost:3306','user'=>'admin','passwd'=>'hellobb','db'=>'serverData'),
//	    'slave'  => array(
//	                      array('host'=>'localhost:3306','user'=>'admin','passwd'=>'hellobb','db'=>'serverData'),
//	                      array('host'=>'localhost:3306','user'=>'admin','passwd'=>'hellobb','db'=>'serverData')
//	                      )
//	                  );

	$db_config = array(
	    'master' => array('host'=>'222.184.24.157','user'=>'ecore','passwd'=>'ecoredata','db'=>'sqlecore'),
	    'slave'  => array(
	                      array('host'=>'222.184.24.157','user'=>'ecore','passwd'=>'ecoredata','db'=>'sqlecore'),
	                      array('host'=>'222.184.24.157','user'=>'ecore','passwd'=>'ecoredata','db'=>'sqlecore')
	                      )
	                  );

class MySQL
{
    
    private static $_instance = null;//数据库连接实例
    private static $_master = null;//主数据库连接实例
    private static $_slave = null;//重数据库连接实例
    
    public $_config = array();//数据库连接配置信息
    public $_res = null;//查询实例句柄
    public $_flag = '';//标识当前语句是在主还是重数据库上执行
    public $_link = null;    
    /**
     * 单实例
     * Enter description here ...
     * @param unknown_type $dbname
     * @param unknown_type $mode
     */
    public static function & getInstance($dbname='',$mode='rw'){
        if (is_null(self::$_instance)){
            self::$_instance = new self();
            self::$_instance->__getConf();
            self::$_instance->connect($dbname,$mode);
        }
        
        return self::$_instance;
    }
    /**
     * 获取数据库配置信息
     * Enter description here ...
     */
    public function __getConf(){
        global $db_config;
        $this->_config['master'] = $db_config['master'];
        $this->_config['slave'] = $db_config['slave'];
    }    
    /**
     * 数据库连接
     * Enter description here ...
     * @param $dbname 指定连接的数据库名，默认情况下连接配置文件的库
     * @param $mode rw表示连接主库，r-w表示读写分离   
     */
    public function connect($dbname='',$mode = 'rw'){
        if($mode == 'rw'){
            if(is_null(self::$_master)){
                $this->_master = $this->_slave = $this->conn_master($dbname);
            }
        }else{
            if(is_null(self::$_master)){
                $this->_master = $this->conn_master($dbname);
            }
            if(is_null(self::$_slave)){
                $this->_slave = $this->conn_slave($dbname);
            }
        }
    }
    /**
     * 连接到主数据库服务器
     * Enter description here ...
     */
    public function conn_master($dbname=''){
        $_link = mysql_connect($this->_config['master']['host'],$this->_config['master']['user'],$this->_config['master']['passwd'],true) or die ("Connect ".$this->_config['master']['host']." fail.");
        mysql_select_db(empty($dbname)?$this->_config['master']['db']:$dbname,$_link) or die(" The DB name ".$this->_config['master']['db']." is not exists.");
        mysql_query("set names utf8",$_link);
        return $_link;
    }    
    /**
     * 连接到从数据库服务器
     * Enter description here ...
     */
    public function conn_slave($dbname=''){
        $offset = rand(0,count($this->_config['slave'])-1);
        $_link = @mysql_connect($this->_config['slave'][$offset]['host'],$this->_config['slave'][$offset]['user'],$this->_config['slave'][$offset]['passwd'],true) or die(" Connect ".$this->_config['slave'][$offset]['host']." fail.");
        mysql_select_db(empty($dbname)?$this->_config['slave'][$offset]['db']:$dbname,$_link) or die(" The DB name ".$this->_config['slave'][$offset]['db']." is not exists.");
        mysql_query("set names utf8",$_link);
        return $_link;
    }
    /**
     * 执行数据库查询
     * Enter description here ...
     * @param string $sql
     */
    public function query($sql,$master=true){    	 
        if($master == true || (substr(strtolower($sql),0,6) != 'select') && $master == false){
            $this->_res = mysql_query($sql,$this->_master);
            if(!$this->_res){
                $this->_error[] = mysql_error($this->_master);
            }
            $this->_flag = 'master';
            $this->_link = $this->_master;
        } else {
            $this->_res = mysql_query($sql,$this->_slave);
            if(!$this->_res){
                $this->_error[] = mysql_error($this->_slave);
            }
            $this->_flag = 'slave';
            $this->_link = $this->_slave;
        }
        return $this->_res;
    }    
    /**
     * 获取单行记录
     * Enter description here ...
     * @param mixed $rs
     */
    public function get($rs=''){
        if(empty($rs)){
            $rs = $this->_res;
        }
        return mysql_fetch_row($rs);
    }    
    /**
     * 获取多行记录
     * Enter description here ...
     * @param mixed $rs
     * @param  $result_type
     */
    public function fetch($rs = ''){
        if(empty($rs)){
            $rs = $this->_res;
        }
        return mysql_fetch_array($rs,MYSQL_ASSOC);
    }    
    /**
     * 插入数据
     * Enter description here ...
     * @param unknown_type $sql
     */
    public function add($sql){
        $rs = $this->query($sql);
        if($rs)
            return mysql_insert_id($this->_link);
        return false;
    }    
    /**
     * 更新数据
     * Enter description here ...
     * @param unknown_type $sql
     */
    public function update($sql){
        if(empty($sql)) return false;
        $rs = $this->query($sql);
        if($rs)
            return $this->fetchNum();
        return false;
    }
    /**
     * 获取上一条语句影响的行数
     * Enter description here ...
     */
    public function fetchNum(){
        return mysql_affected_rows($this->_link);
    }    
    /**
     * 析构函数,释放数据库连接资源
     * Enter description here ...
     */
    public function __destruct(){
    	  try
	      {
	      	if (!is_null($this->_link)) {
            mysql_close($this->_link);         
          }
        }
        catch(Exception $e)
				{
					//;
				}
			}
}
?>